// BlogCollectionService.java
package net.web.service;

import net.web.dao.BlogCollectionDao;
import net.web.entity.BlogCollection;
import net.web.entity.MessageNotification;
import net.web.service.MessageNotificationService;
import net.web.dao.BlogDao;

public class BlogCollectionService {
    private BlogCollectionDao collectionDao = new BlogCollectionDao();
    private MessageNotificationService messageNotificationService = new MessageNotificationService();
    private BlogDao blogDao = new BlogDao();

    // 在 BlogCollectionService 的 collectBlog 方法中添加消息通知逻辑
    public boolean collectBlog(int blogId, int userId) {
        if (!collectionDao.isCollected(blogId, userId)) {
            BlogCollection collection = new BlogCollection();
            collection.setBlogId(blogId);
            collection.setUserId(userId);
            boolean result = collectionDao.insert(collection);
            if (result) {
                Integer blogAuthorId = blogDao.getAuthorIdByBlogId(blogId);
                if (blogAuthorId != null && blogAuthorId != userId) {
                    MessageNotification notification = new MessageNotification();
                    notification.setRecipientId(blogAuthorId);
                    notification.setSenderId(userId);
                    notification.setBlogId(blogId);
                    notification.setMessageType("collection");
                    messageNotificationService.sendNotification(notification);
                }
            }
            return result;
        }
        return false;
    }

    public boolean uncollectBlog(int blogId, int userId) {
        return collectionDao.delete(blogId, userId);
    }

    public boolean isCollected(int blogId, int userId) {
        return collectionDao.isCollected(blogId, userId);
    }

    public int getCollectionCount(int blogId) {
        return collectionDao.getCollectionCount(blogId);
    }

    public java.util.List<net.web.entity.Blog> getCollectionsByUserId(int userId) {
        return collectionDao.getCollectionsByUserId(userId);
    }
}